package org.dinnermate.log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class SimpleFormatter extends Formatter
{
	@Override
	public String format( LogRecord record )
	{
		StringBuffer sb = new StringBuffer();

		// Level
		sb.append(record.getLevel().getName() + " :: ");

		// Time
		Calendar cal = new GregorianCalendar();
		cal.setTimeInMillis(record.getTime());
		sb.append(cal.get(Calendar.HOUR_OF_DAY) + ":"); // hour
		sb.append(cal.get(Calendar.MINUTE) + "."); // min
		sb.append(cal.get(Calendar.SECOND)); // second
		sb.append(" - ");

		// Class
		sb.append(record.getSourceClassName() + ".");

		// Method
		sb.append(record.getSourceMethodName() + "\n");

		// Message
		sb.append("Msg: " + record.getMessage() + "\n");

		// Throwable
		if (record.getThrown() != null)
		{
			try
			{
				StringWriter sw = new StringWriter();
				PrintWriter pw = new PrintWriter(sw);
				record.getThrown().printStackTrace(pw);
				pw.close();
				sb.append(sw.toString());
			}
			catch (Exception ex)
			{}
		}

		return sb.toString();
	}

}
